package com.ming.stringstudy.feedbacksystem.controller;

import com.ming.stringstudy.feedbacksystem.anno.PermissionLog;
import com.ming.stringstudy.feedbacksystem.entity.Role;
import com.ming.stringstudy.feedbacksystem.pojo.PageBean;
import com.ming.stringstudy.feedbacksystem.pojo.Result;
import com.ming.stringstudy.feedbacksystem.service.IRoleService;
import com.ming.stringstudy.feedbacksystem.utils.PermissionConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author aming
 * @since 2025-01-19
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @PermissionLog(PermissionConstants.ROLE_MANGE)
    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       @RequestParam(required = false) Integer id,
                       @RequestParam(required = false) String roleType){
        try {
            Role role = new Role();
            role.setId(id);
            role.setRoleType(roleType);
            PageBean pageBean = roleService.page(page, pageSize, role);
            return Result.success(pageBean);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("服务器内部错误");
        }
    }
    @PermissionLog(PermissionConstants.ROLE_MANGE)
    @PostMapping
    public Result addRole(@RequestBody Role role){
        try {
            int result = roleService.addRole(role);
            if (result == 0){
                return Result.error("添加失败");
            }else {
                return Result.success("添加成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("服务器内部错误");
        }
    }
    @PermissionLog(PermissionConstants.ROLE_MANGE)
    @PutMapping
    public Result updateRole(@RequestBody Role role){
        try {
            int result = roleService.updateRole(role);
            if (result == 0){
                return Result.error("修改失败");
            }else {
                return Result.success("修改成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("服务器内部错误");
        }
    }
    @PermissionLog(PermissionConstants.ROLE_MANGE)
    @DeleteMapping("/{ids}")
    public Result deleteRole(@PathVariable List<Integer> ids){
        try {
            int result = roleService.deleteRole(ids);
            if (result == 0){
                return Result.error("删除失败");
            }else {
                return Result.success("删除成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("服务器内部错误");
        }
    }
}

